/*
 * 
 */
package server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ServerComunicator
{
	
	/** The con. */
	private Connection con;
	
	public ServerComunicator ()
	{		 
	}
	/**
	 * Connect to MySql data base.
	 *
	 * @param scheme the MySql scheme
	 * @param username the MySql username
	 * @param password the MySql password
	 * @return true, if successful
	 */
	public boolean connectToDB(String scheme ,String username , String password )
	{
		try
		{
			//connect to MySql DB using JDBC and connection information from User
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection("jdbc:mysql://localhost/"+scheme, username ,password);	
			return true;
		}
		catch (Exception E)
		{
			E.printStackTrace();
			return false;
		}
	}
	
	/**
	 * Gets the DB connection.
	 *
	 * @return the DB connection
	 */
	public Connection getDBConnection ()
	{
		return con;
	}
	
	/**
	 * Prints the sql exception.
	 *
	 * @param ex the SQLException
	 * 
	 */
	public static void printSQLException(SQLException ex) 
	{
	    for (Throwable e : ex) 
	    {
	        if (e instanceof SQLException) 
	        {

	                e.printStackTrace(System.err);
	                System.err.println("SQLState: " +
	                    ((SQLException)e).getSQLState());

	                System.err.println("Error Code: " +
	                    ((SQLException)e).getErrorCode());

	                System.err.println("Message: " + e.getMessage());

	                Throwable t = ex.getCause();
	                while(t != null) 
	                {
	                    System.out.println("Cause: " + t);
	                    t = t.getCause();
	                }
	        }
	    }
	}
}
